package com.jy.adscm.operation.cg.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.cg.bean.*;
import com.jy.adscm.operation.cg.mapper.mysql.*;
import com.jy.adscm.operation.cg.service.CgBomSupplierService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.mscyb.bean.JqylMaterial;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqylMaterialMapper;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.utils.AdscmDateUtils;
import com.jy.adscm.zltx.bean.JyDrawingList;
import com.jy.adscm.zltx.bean.JyDrawingType;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingListMapper;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingTypeMapper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * bom与供应商 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-07-07
 */
@Service
public class CgBomSupplierServiceImpl extends ServiceImpl<CgBomSupplierMapper, CgBomSupplier> implements CgBomSupplierService {
     @Autowired
    CgBomMapper cgBomMapper;
     @Autowired
     CgSupplierMapper cgSupplierMapper;
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
    JyDrawingTypeMapper jyDrawingTypeMapper;
     @Autowired
     CgBomSupplierMapper cgBomSupplierMapper;
     @Autowired
    JyDrawingListMapper jyDrawingListMapper;
     @Autowired
    JyCodeMapper jyCodeMapper;
     @Autowired
    CgMaterialMapper cgMaterialMapper;
    @Override
    public List<CgBomSupplier> selectByBid(Integer bid) {
        Wrapper<CgBomSupplier> wrapper=new EntityWrapper<>();
        wrapper.eq("bid",bid);
        List<CgBomSupplier> cgBomSuppliers = super.selectList(wrapper);
        for (CgBomSupplier cgBomSupplier : cgBomSuppliers) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBomSupplier.getBid(),null)){
                CgBom cgBom = cgBomMapper.selectById(cgBomSupplier.getBid());
                cgBomSupplier.setBomName(cgBom);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBomSupplier.getSid(),null)){
                CgSupplier cgSupplier = cgSupplierMapper.selectById(cgBomSupplier.getSid());
                cgBomSupplier.setSupplierName(cgSupplier.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBomSupplier.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(cgBomSupplier.getCreator());
                cgBomSupplier.setCreatorName(jyUser.getName());
            }
        }
        return cgBomSuppliers;
    }

    @Override
    public List<JyDrawingType> selectDrawBoms() {
        List<JyDrawingType> jyDrawingTypes = jyDrawingTypeMapper.selectList(null);
        for (JyDrawingType jyDrawingType : jyDrawingTypes) {
            Wrapper<JyDrawingList> wrapper =new EntityWrapper<>();
            wrapper.eq("parent_id",0);
            wrapper.eq("drawing_type",jyDrawingType.getId());

            List<JyDrawingList> jyDrawingLists = jyDrawingListMapper.selectList(wrapper);
            if (jyDrawingLists!=null&&jyDrawingLists.size()>0 ){
                JyDrawingList jyDrawingList = jyDrawingLists.get(0);
                jyDrawingType.setName(jyDrawingType.getName()+":"+jyDrawingList.getDrawingName());
            }
        }

        return jyDrawingTypes;
    }

    @Override
    public Page<CgBom> selectBoms(Page page, QueryCgBom queryCgBom ) {
        Wrapper<CgBom> wrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgBom.getMachineType(),null)){
            wrapper.eq("machine_type",queryCgBom.getMachineType());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgBom.getBomName(),null)){
            wrapper.like("name",queryCgBom.getBomName());
        }
        wrapper.eq("enable",1);
        RowBounds rowBounds =new RowBounds((queryCgBom.getCurrentPage()-1)*queryCgBom.getPageSize(), queryCgBom.getPageSize());
        List<CgBom> cgBoms = cgBomMapper.selectPage(rowBounds, wrapper);
        for (CgBom cgBom : cgBoms) {
            Wrapper<CgBomSupplier> cgBomSupplierWrapper=new EntityWrapper<>();
            cgBomSupplierWrapper.eq("bid",cgBom.getId());
            List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(cgBomSupplierWrapper);
            if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                cgBom.setIsHas("1");
                cgBom.setSuppilerNums(cgBomSuppliers.size()+"");
                CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                cgBom.setCgBomSupplier(cgBomSupplier);
                CgSupplier cgSupplier = cgSupplierMapper.selectById(cgBomSupplier.getSid());
                if (AdscmBaseUtils.getInstance().isNotEmpty(cgSupplier,null)){
                    if (AdscmBaseUtils.getInstance().isNotEmpty(cgSupplier.getPaymentMethod(),null)){
                        JyCode jyCode =new JyCode();
                        jyCode.setCodeType(6);
                        jyCode.setCode(cgSupplier.getPaymentMethod().toString());
                        jyCode= jyCodeMapper.selectOne(jyCode);
                        if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                            cgSupplier.setPaymentMethodName(jyCode.getName());
                        }
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(cgSupplier.getTransporttationMethod(),null)){
                        JyCode jyCode =new JyCode();
                        jyCode.setCodeType(7);
                        jyCode.setCode(cgSupplier.getTransporttationMethod().toString());
                        jyCode= jyCodeMapper.selectOne(jyCode);
                        if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                            cgSupplier.setTransporttationMethodName(jyCode.getName());
                        }
                    }



                    cgBom.setCgSupplier(cgSupplier);
                }
            }else {
                cgBom.setIsHas("0");
                cgBom.setSuppilerNums("");
            }
          //根据机器型号添加机器类型
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getMachineType(),null)){
                JyDrawingType jyDrawingType = jyDrawingTypeMapper.selectById(cgBom.getMachineType());
                cgBom.setMachineTypeName(jyDrawingType.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getDrawId(),null)){
                JyDrawingList jyDrawingList = jyDrawingListMapper.selectById(cgBom.getDrawId());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyDrawingList,null)){
                    cgBom.setDrawIdName(jyDrawingList.getDrawingName());
                }else {
                    cgBom.setDrawIdName(cgBom.getDrawId().toString());
                }
            }
            //计算总价
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getCgBomSupplier(),null)){
                cgBom.setTotal(cgBom.getNums()*cgBom.getCgBomSupplier().getPrice());
            }

        }


        page.setRecords(cgBoms);
        page.setTotal(cgBomMapper.selectCount(wrapper));
        return page;
    }
    @Deprecated
    @Override
    public BomList selectBom(QueryCgBom queryCgBom) { BomList bomList =new BomList();
        Wrapper<CgBom> wrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgBom.getMachineType(),null)){
            wrapper.eq("machine_type",queryCgBom.getMachineType());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgBom.getBomName(),null)){
            wrapper.like("name",queryCgBom.getBomName());
        }
        wrapper.eq("enable",1);
        List<CgBom> cgBoms = cgBomMapper.selectList(wrapper);
        List<CgBom> result=new ArrayList<>();
        for (CgBom cgBom : cgBoms) {
            Wrapper<CgBomSupplier> cgBomSupplierWrapper=new EntityWrapper<>();
            cgBomSupplierWrapper.eq("bid",cgBom.getId());
            List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(cgBomSupplierWrapper);
            if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                cgBom.setIsHas("1");
                cgBom.setSuppilerNums(cgBomSuppliers.size()+"");
                CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                if(AdscmBaseUtils.getInstance().isNotEmpty( cgBomSupplier.getType(),null)){
                    if (cgBomSupplier.getType()==1){
                        cgBomSupplier.setTypeName("月采购");
                    }
                    if (cgBomSupplier.getType()==2){
                        cgBomSupplier.setTypeName("年采购");
                    }
                }
                cgBom.setCgBomSupplier(cgBomSupplier);
                CgSupplier cgSupplier = cgSupplierMapper.selectById(cgBomSupplier.getSid());
                if (AdscmBaseUtils.getInstance().isNotEmpty(cgSupplier,null)){
                    if (AdscmBaseUtils.getInstance().isNotEmpty(cgSupplier.getPaymentMethod(),null)){
                        JyCode jyCode =new JyCode();
                        jyCode.setCodeType(6);
                        jyCode.setCode(cgSupplier.getPaymentMethod().toString());
                        jyCode= jyCodeMapper.selectOne(jyCode);
                        if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                            cgSupplier.setPaymentMethodName(jyCode.getName());
                        }
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(cgSupplier.getTransporttationMethod(),null)){
                        JyCode jyCode =new JyCode();
                        jyCode.setCodeType(7);
                        jyCode.setCode(cgSupplier.getTransporttationMethod().toString());
                        jyCode= jyCodeMapper.selectOne(jyCode);
                        if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                            cgSupplier.setTransporttationMethodName(jyCode.getName());
                        }
                    }
                    cgBom.setCgSupplier(cgSupplier);
                }
            }else {
                cgBom.setIsHas("0");
                cgBom.setSuppilerNums("");
            }
            //根据机器型号添加机器类型
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getMachineType(),null)){
                JyDrawingType jyDrawingType = jyDrawingTypeMapper.selectById(cgBom.getMachineType());
                cgBom.setMachineTypeName(jyDrawingType.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getDrawId(),null)){
                JyDrawingList jyDrawingList = jyDrawingListMapper.selectById(cgBom.getDrawId());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyDrawingList,null)){
                    cgBom.setDrawIdName(jyDrawingList.getDrawingName());
                }else {
                    cgBom.setDrawIdName(cgBom.getDrawId().toString());
                }
            }
            //计算总价
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getCgBomSupplier(),null)){
                if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getNums(),null)){
                    if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getCgBomSupplier().getPrice(),null)){
                        cgBom.setTotal(cgBom.getCgBomSupplier().getPrice()*cgBom.getNums());
                    }
                }

            }
             if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgBom.getSupplier(),null)){
               if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getCgSupplier(),null)){
                  if (queryCgBom.getSupplier().equals(cgBom.getCgSupplier().getId())){
                      result.add(cgBom);
                  }
               }
             }else {
                 result.add(cgBom);
             }
        }
        float temp=0f;
        for (CgBom cgBom : result) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getTotal(),null)){
                temp=temp+cgBom.getTotal();
            }
        }
         bomList.setCgBoms(result);
        bomList.setTotals(String.valueOf(temp));
        return bomList;
    }

    @Override
    public BomList querybom(QueryCgBom queryCgBom) {
        List<CgBom> cgBoms = cgBomMapper.selectBoms(queryCgBom);
        float temp=0f;
        float tempminPurchaseTotals=0f;
        float tempNoTotal=0f;
        for (CgBom cgBom : cgBoms) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getTotal(),null)){
                temp=temp+ cgBom.getTotal();
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getMinPurchaseTotal(),null)){
                tempminPurchaseTotals=tempminPurchaseTotals+ cgBom.getMinPurchaseTotal();
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getNo(),null)){
                if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getTotal(),null)){
                        tempNoTotal=tempNoTotal+ cgBom.getTotal()*cgBom.getNo();
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getCgBomSupplier(),null)){
                cgBom.setIsHas("1");
            }else {
                cgBom.setIsHas("0");
            }
        }
        BomList  bomList=new BomList();
        DecimalFormat df =new DecimalFormat();
        df.setGroupingSize(3);

        bomList.setCgBoms(cgBoms);
        bomList.setTotal( cgBoms.size());
        bomList.setMinPurchaseTotals(df.format(tempminPurchaseTotals));
        bomList.setTotals(df.format(temp));
        bomList.setNoTotals(df.format(tempNoTotal));
        return bomList;
    }




    @Override
    public List<CgSupplier> selectSupplierbyId(QueryCgBom queryCgBom) {
        List<CgSupplier> suppliers =new ArrayList<>();
        BomList querybom = queryboms(queryCgBom);
        List<CgBom> cgBoms = querybom.getCgBoms();
        HashMap<Long ,CgSupplier > map=new HashMap<>();
        for (CgBom cgBom : cgBoms) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getCgSupplier(),null)){
                CgSupplier cgSupplierfromMap = map.get(cgBom.getCgSupplier().getId());
                if (!AdscmBaseUtils.getInstance().isNotEmpty(cgSupplierfromMap,null)){
                    suppliers.add(cgBom.getCgSupplier());
                    map.put(cgBom.getCgSupplier().getId(),cgBom.getCgSupplier());
                }

            }
        }
        return suppliers;
    }

    @Override
    public void updateBom(CgBom cgBom) {
        Integer integer = cgBomMapper.updateById(cgBom);


    }
    @Autowired
    CgTaskDetailMapper cgTaskDetailMapper;
    @Autowired
    JqylMaterialMapper jqylMaterialMapper;
    @Autowired
    CgTaskMapper cgTaskMapper;
    @Override
    public void test() {
        List<CgTaskDetail> cgTaskDetails = cgTaskDetailMapper.selectList(null);
        for (CgTaskDetail cgTaskDetail : cgTaskDetails) {
            CgTask cgTask = cgTaskMapper.selectById(cgTaskDetail.getTid());
            Long bomName = cgTaskDetail.getBomName();
            Wrapper<JqylMaterial> wrapper=new EntityWrapper<>();
            wrapper.eq("RESOURCENAME",bomName);
            List<JqylMaterial> jqylMaterials = jqylMaterialMapper.selectList(wrapper);
            if (jqylMaterials!=null&&jqylMaterials.size()>0 ){
                JqylMaterial jqylMaterial = jqylMaterials.get(0);
                jqylMaterial.setTotals(cgTaskDetail.getActPrice()*cgTaskDetail.getActNums());
                jqylMaterial.setPrice(cgTaskDetail.getPrice());
                jqylMaterial.setBusinesstype(cgTask.getSupplier().toString());
                jqylMaterialMapper.updateById(jqylMaterial);
            }
        }
    }

    @Override
    public BomList queryboms(QueryCgBom queryCgBom) {
        List<CgBom> cgBoms = cgBomMapper.queryBoms(queryCgBom);
        float temp=0f;
        float tempminPurchaseTotals=0f;
        float tempNoTotal=0f;
        for (CgBom cgBom : cgBoms) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getTotal(),null)){
                temp=temp+ cgBom.getTotal();
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getMinPurchaseTotal(),null)){
                tempminPurchaseTotals=tempminPurchaseTotals+ cgBom.getMinPurchaseTotal();
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getCgMaterial(),null)){
                if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getCgMaterial().getNo(),null)){
                    if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getTotal(),null)){
                        tempNoTotal=tempNoTotal+ cgBom.getTotal()*cgBom.getCgMaterial().getNo();
                    }
                }

            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getCgBomSupplier(),null)){
                cgBom.setIsHas("1");
            }else {
                cgBom.setIsHas("0");
            }
        }
        BomList  bomList=new BomList();
        DecimalFormat df =new DecimalFormat();
        df.setGroupingSize(3);
        List<CgBom> result=new ArrayList<>();
        int start=0;
        int size = cgBoms.size();
        Integer pageSize = queryCgBom.getPageSize();
        Integer currentPage = queryCgBom.getCurrentPage();
        if (pageSize*currentPage>size){
            size=size;
        }else {
            size=pageSize*currentPage;
        }
        start=(currentPage-1)*pageSize;
        for (int i = start; i < size; i++) {
            result.add(cgBoms.get(i));
        }
        bomList.setCgBoms(result);
        bomList.setTotal( cgBoms.size());
        bomList.setMinPurchaseTotals(df.format(tempminPurchaseTotals));
        bomList.setTotals(df.format(temp));
        bomList.setNoTotals(df.format(tempNoTotal));
        return bomList;
    }
}
